﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ciiattack
{
    public class MissileManager
    {
        private Texture2D missileTexture;
        private Vector2 missileOrigin;
        private SpriteBatch spriteBatch;
        private List<Missile> missiles;
        private Single fireCounter;
        private EnemyManager enemyManager;
        public int score = 0;

        public List<Missile> Missile
        {
            get { return missiles; }
        }

        public MissileManager(Texture2D missileTexture, Vector2 missileOrigin, SpriteBatch spriteBatch, EnemyManager enemyManager)
        {
            this.missileTexture = missileTexture;
            this.missileOrigin = missileOrigin;
            this.spriteBatch = spriteBatch;
            this.enemyManager = enemyManager;
            missiles = new List<Missile>(64);

            for (Int32 i = 0; i < 64; i++)
            {
                missiles.Add(new Missile(this.missileTexture, this.missileOrigin, this.spriteBatch, this.enemyManager));
            }

            fireCounter = 0.0f;
        }

        public int Update(GameTime gameTime, int resultat)
        {
            // Collisions...
            foreach (Missile i in missiles)
            {
                i.Update(gameTime);

                if (i.IsActive)
                {
                    foreach (Enemy e in this.enemyManager.Enemies)
                    {
                        if (i.BoundingRectangle.Intersects(e.BoundingRectangle))
                        {
                            resultat = resultat + 10;
                            e.OnHit();
                            i.OnHit();
                        }
                    }
                }
            }

            if (fireCounter > 0.0f)
            {
                fireCounter -= (Single)gameTime.ElapsedGameTime.TotalMilliseconds;
            }
            else
            {
                if (fireCounter < 0.0f)
                {
                    fireCounter = 0.0f;
                }
            }
            return resultat;
        }

        public void Draw(GameTime gameTime, SpriteFont scr, int resultat)
        {
            foreach (Missile i in missiles)
            {
                if (i.IsActive)
                {
                    i.Draw(gameTime);
                    this.spriteBatch.DrawString(scr, resultat.ToString(), new Vector2(10, 20), Color.White);
                }
            }
        }

        public void Fire(Vector2 position)
        {
            if (this.fireCounter == 0.0f)
            {
                foreach (Missile i in missiles)
                {
                    if (!i.IsActive) // NOT i.IsActive
                    {
                        i.Fire(position);

                        this.fireCounter = 200.0f;

                        break;
                    }
                }
            }
        }
    }
}